package com.szxy.collectdemo;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: zwz
 * @Date:2020/10/23 16:45
 * @Description: ArrayList 线程不安全举例
 **/
public class ContainerDemo {
    public static void main(String[] args) {
        // ArrayList<Integer> list = new ArrayList<>(100);
        // 解决 ArrayList 线程安全问题
        // 1. Vector<Integer> list = new Vector<>();
        // 2. List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>(100));
        // 3. new CopyOnWriteArrayList<>();
        CopyOnWriteArrayList<Object> list = new CopyOnWriteArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                list.add(random.nextInt(50));
                System.out.println(list);
            }).start();
        }
    }

}
